import java.util.Scanner;
import java.util.zip.CheckedOutputStream;

public class Test02 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T  = in.nextInt();
        while(T-- > 0){
            int n = in.nextInt();
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = in.nextInt();
            }
            int maxSum = findMax(arr);
            System.out.println(maxSum);
        }
    }

    private static int findMax(int[] arr){
        int maxSum = 0;
        int currentSum = 0;
        int[] count = new int[arr.length - 1];
        for (int num : arr) {
            count[num]++;
        }
        int mex;
        for(mex = 0; mex < arr.length + 1; mex++){
            if(count[mex] == 0){
                break;
            }
        }
        for (int i = 0; i < arr.length; i++) {
            if(count[arr[i]] == 1){
                count[arr[i]]--;
                if(mex == arr[i]){
                    while (mex < arr.length + 1 && count[mex] != 0){
                        mex++;
                    }
                }
                currentSum = Math.max(mex, currentSum + mex);
                maxSum = Math.max(maxSum, currentSum);
            }else{
                currentSum = Math.max(currentSum , 0);
            }
        }
        return maxSum;
    }
}
